package com.ws.lv.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.lv.bean.*;
import com.ws.lv.service.*;
import com.ws.lv.util.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/route")
public class RouteController {
    @Autowired
    RouteService routeService;
    @Autowired
    TypeService typeService;
    @Autowired
    ThemeService themeService;
    @Autowired
    SupplierService supplierService;
    @Autowired
    SlideshowService slideshowService;
    @Autowired
    ImageService imageService;
    @Autowired
    Order_infoService order_infoService;
    @Autowired
    SaveService saveService;


    //后台查询所有的旅游线路+多条件+分页
    @RequestMapping("/selectRoute")
    public String selectRoute(Model model, RouteBySelect routeBySelect){
        System.out.println("根据条件查询旅游线路");
        List<Type> types = typeService.selectTypeAll();
        model.addAttribute("types",types);

        List<Theme> themes = themeService.selectThemeAll();
        model.addAttribute("themes",themes);

        List<Supplier> suppliers = supplierService.selectSupplierAll();
        model.addAttribute("suppliers",suppliers);

        PageHelper pageHelper = new PageHelper();//分页帮助类
        pageHelper.startPage(routeBySelect.getCurrentPage(), routeBySelect.getPageSize());
        List<Route> routes = routeService.selectRoute(routeBySelect);
        PageInfo pageInfo = new PageInfo(routes);

        model.addAttribute("routes",routes);
        model.addAttribute("routeBySelect",routeBySelect);
        model.addAttribute("pageInfo",pageInfo);

        return "adminpage/route/list";
    }

    //去添加页面
    @RequestMapping("/toAddRoute")
    public String toAddRoute(Model model){
        List<Type> types = typeService.selectTypeAll();
        model.addAttribute("types",types);

        List<Theme> themes = themeService.selectThemeAll();
        model.addAttribute("themes",themes);

        List<Supplier> suppliers = supplierService.selectSupplierAll();
        model.addAttribute("suppliers",suppliers);

        return "adminpage/route/add";
    }
    //添加旅游线路
    @RequestMapping("/insertRoute")
    public String insertRoute(Route route, MultipartFile image,Slideshow slideshow,MultipartFile mainImage) {
        System.out.println("增加旅游线路");
        try {
            System.out.println(route);
            System.out.println(slideshow);
            if (slideshow != null){
                if (mainImage.getSize() > 0){
                    String uuidMain = UUID.randomUUID().toString().replaceAll("-", "");
                    String suffixMain = mainImage.getOriginalFilename().split("\\.")[1];
                    route.setRouth_mainImage(uuidMain+"."+suffixMain);
                    int i = routeService.insertRoute(route);
                    mainImage.transferTo(new File(Res.mainFileDir+uuidMain+"."+suffixMain));

                    if (image.getSize() > 0){
                        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                        String suffix = image.getOriginalFilename().split("\\.")[1];
                        slideshow.setSlideshow_url(uuid+"."+suffix);
                        slideshow.setSlideshow_routeId(route.getRoute_id());
                        slideshowService.insertSlideshow(slideshow);
                        image.transferTo(new File(Res.fileDir+uuid+"."+suffix));
                    }

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/route/selectRoute";
    }

    //逻辑删除旅游线路
    @RequestMapping("/softDeleteRoute")
    public String softDeleteRoute(Integer routeId){
        System.out.println("逻辑删除旅游线路");
        int i = routeService.softDeleteRoute(routeId);
        return "redirect:/route/selectRoute";
    }

    //删除多个旅游线路
    @RequestMapping("/deleteRoutes")
    public String deleteRoutes(String routeIds){
        System.out.println("删除多个旅游线路");
        ArrayList<Integer> list = new ArrayList<>();
        if (routeIds!=null && routeIds.trim().length()>0){
            String[] split = routeIds.split(",");
            for (String s : split) {
                list.add(Integer.parseInt(s));
            }
            System.out.println(list);
            //删除订单与收藏
            for (Integer routeId : list){
                order_infoService.deleteOrder_infoByRouteId(routeId);
                saveService.deleteSaveByRouteId(routeId);
            }

            //删除轮播图
            List<Slideshow> slideshows = slideshowService.selectSlideshowByRouteId(list);
            if (slideshows!=null){
                for (Slideshow slideshow : slideshows){
                    File file = new File(Res.fileDir+slideshow.getSlideshow_url());
                    if (file.exists()){
                        file.delete();
                        System.out.println("删除轮播图图片");
                    }
                }
                System.out.println("删除轮播————————————————————————————————");
                int j = slideshowService.deleteSlideshows(list);
            }
            //删除附图
            for (Integer routeId : list){
                List<Image> images = imageService.selectImageByRouteId(routeId);
                for (Image image : images){
                    File file = new File(Res.fuTuFileDir+image.getImage_url());
                    if (file.exists()){
                        file.delete();
                        System.out.println("删除附图图片");
                    }
                }
            }
            System.out.println("删除附图————————————————————————————————");
            imageService.deleteImageByRouteIds(list);



            //删除主图
            for (Integer id : list){
                Route route = routeService.selectRouteById(id);
                File file = new File(Res.mainFileDir+route.getRouth_mainImage());
                if (file.exists()){
                    file.delete();
                    System.out.println("删除主图");
                }
            }

            System.out.println("删除线路————————————————————————————————");
            int i = routeService.deleteRoutes(list);
        }
        return "redirect:/route/selectRoute";
    }

    //去修改页面
    @RequestMapping("/toUpdateRoute")
    public String toUpdateRoute(int routeId,Model model){
        System.out.println("去修改页面");
        Slideshow slide = slideshowService.selectSlideshowByRoute_id(routeId);
        model.addAttribute("slide",slide);

        List<Type> types = typeService.selectTypeAll();
        model.addAttribute("types",types);

        List<Theme> themes = themeService.selectThemeAll();
        model.addAttribute("themes",themes);

        List<Supplier> suppliers = supplierService.selectSupplierAll();
        model.addAttribute("suppliers",suppliers);

        Route route = routeService.selectRouteById(routeId);
        model.addAttribute("route",route);

        return "adminpage/route/update";
    }

    //修改旅游线路
    @RequestMapping("/updateRoute")
    public String updateRoute(Route route,Slideshow slideshow,MultipartFile image,Model model,MultipartFile mainImage){
        System.out.println("修改旅游线路");
        System.out.println(route+"-------------");
        try {
            Slideshow slide = slideshowService.selectSlideshowByRoute_id(route.getRoute_id());
            if (mainImage.getSize() > 0){
                System.out.println("更换新主图");
                File file = new File(Res.mainFileDir+route.getRouth_mainImage());
                if (file.exists()){
                    file.delete();
                    System.out.println("删除原主图");
                }
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                String suffix = mainImage.getOriginalFilename().split("\\.")[1];
                route.setRouth_mainImage(uuid+"."+suffix);
                mainImage.transferTo(new File(Res.mainFileDir+uuid+"."+suffix));
            }
            if (route.getRouth_is_slideshow() == 1){
                if (image.getSize() > 0){
                    if (slide!=null){
                        System.out.println("轮播修改图片————————————————————————————————");
                        System.out.println("轮播修改前--------------"+slide);
                        File file = new File(Res.fileDir + slide.getSlideshow_url());
                        if (file.exists()){
                            file.delete();
                            System.out.println("删除原轮播图片");
                        }
                        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                        String suffix = image.getOriginalFilename().split("\\.")[1];
                        slide.setSlideshow_url(uuid + "." + suffix);
                        slide.setSlideshow_order(slideshow.getSlideshow_order());
                        System.out.println("轮播修改后--------------"+slide);
                        image.transferTo(new File(Res.fileDir + uuid + "." + suffix));
                        int i = slideshowService.updateSlideshow(slide);
                    }else {
                        System.out.println("新增轮播图片————————————————————————————————");
                        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                        String suffix = image.getOriginalFilename().split("\\.")[1];
                        slideshow.setSlideshow_routeId(route.getRoute_id());
                        slideshow.setSlideshow_url(uuid + "." + suffix);
                        slideshow.setSlideshow_order(slideshow.getSlideshow_order());
                        image.transferTo(new File(Res.fileDir+uuid+"."+suffix));
                        int i = slideshowService.insertSlideshow(slideshow);
                    }
                }else {
                    System.out.println("使用原来轮播图片");
                    slide.setSlideshow_order(slideshow.getSlideshow_order());
                    int i = slideshowService.updateSlideshow(slide);
                }
            }else {
                if (slide!=null){
                    File file = new File(Res.fileDir + slide.getSlideshow_url());
                    if (file.exists()){
                        file.delete();
                        System.out.println("删除轮播状态下的图片");
                    }
                    int i = slideshowService.deleteSlideshow(slide.getSlideshow_id());
                    System.out.println("删除轮播图片回到没有轮播状态不变");
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        int i = routeService.updateRoute(route);
        return "redirect:/route/selectRoute";
    }

    
    







}
